/****************************************************************************
**
** Copyright (C) 1992-2009 Nokia. All rights reserved.
**
** This file is part of Qt Jambi.
**
** $BEGIN_LICENSE$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
** $END_LICENSE$

**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

package com.trolltech.autotests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.trolltech.autotests.generated.SqlTableModelSubclass;
/*import com.trolltech.autotests.generated.Relation;
import com.trolltech.autotests.generated.Role;
import com.trolltech.autotests.generated.State;*/

import com.trolltech.qt.QVariant;
import com.trolltech.qt.sql.QSqlDatabase;
import com.trolltech.qt.sql.QSqlField;
import com.trolltech.qt.sql.QSqlRecord;
import com.trolltech.unittests.support.CategorySQL;
import com.trolltech.unittests.support.FilterSQL;

// SQL support is an optional part of API:
// 1) The javac has to compile this package (this is the usual way the
//    test is deselected by having javac just not compile it)
// 2) The ANT testrunner looks over the source code folder for tests the
//    problem is that this class won't load in environment where OpenGL
//    package does not exist.  FIXME
@Category(CategorySQL.class)
public class TestInjectedCodeSQL extends QApplicationTest {

    @BeforeClass
    public static void testInitialize() throws Exception {
        assumeTrue(FilterSQL.detectStatic());
        QApplicationTest.testInitialize(null);
    }

    @SuppressWarnings("unused")  // Used by testSqlTableModelBeforeInsertJava()
    private void receiveBeforeInsert(QSqlRecord record) {
        record.append(new QSqlField("javaInt", QVariant.Int));
        record.setValue("javaInt", 3456);
    }

    @Test
    public void testSqlTableModelBeforeInsertJava() {
        SqlTableModelSubclass stms = new SqlTableModelSubclass();

        stms.beforeInsert.connect(this, "receiveBeforeInsert(QSqlRecord)");
        stms.emitBeforeInsert();

        QSqlRecord record = stms.myRecord();
        assertEquals(3456, record.value("javaInt"));
        assertEquals(3456, QVariant.toInt(record.value("javaInt")));
    }

    @Test
    public void testSqlTableModelBeforeInsertCpp() {
        SqlTableModelSubclass stms = new SqlTableModelSubclass();

        stms.connectBeforeInsert();

        QSqlRecord record = new QSqlRecord();
        stms.beforeInsert.emit(record);

        assertEquals(1234, QVariant.toInt(record.value("cppInt")));
    }

    @Test
    public void testSqlDatabaseDefaultConnection() {
        String defaultConnection = QSqlDatabase.defaultConnection();
        assertEquals("qt_sql_default_connection", defaultConnection);
    }


    public static void main(String args[]) {
        org.junit.runner.JUnitCore.main(TestInjectedCodeSQL.class.getName());
    }
}
